'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:LoginLoginCtrl
 * @description
 * # LoginLoginCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('LoginLoginCtrl', function (DataService, openApi, Util, RongYService, $location, $window, mdfive, DeveiceIdService, $timeout) {
        var userinfo = {};
        var vm = this;
        vm.param = {};
        var clientType = Util.getPlatform() == "android" ? 1 : 2;
        var clientId;
        userinfo = JSON.parse($window.localStorage.getItem("userinfo")) || {};
        vm.saveuser = !!JSON.parse($window.localStorage.getItem("saveuser"));
        vm.login = function () {
            if (!vm.param.accountName) {
                Util.Toast("请输入组织名")
                return;
            }
            if (!vm.param.userName) {
                Util.Toast("请输入用户名")
                return;
            }
            if (!vm.param.passWord) {
                Util.Toast("请输入密码")
                return;
            }
            // 开始时候获取不到
            DeveiceIdService.getDeviceId().then(function (id) {
                if (!id) {
                    // 如果获取不到设备id,就过30秒钟再获取
                    $timeout(DeveiceIdService.sendIdToServe, 30000)
                }
                locallogin(id)
            })
        };
        var locallogin = function (id) {
            var params = {
                clientType: clientType,
                passWord: mdfive(vm.param.passWord),
                userName: encodeURI(vm.param.userName),
                accountName: encodeURI(vm.param.accountName)
            };
            if (id) {
                params.clientId = clientId = id
            }
            DataService.getData(openApi.login, params).then(function (data) {
                var loginData = data;
                userinfo = vm.param;
                $window.localStorage.setItem("userinfo", JSON.stringify(userinfo));
                // 记住密码
                if (vm.saveuser) {
                    $window.localStorage.setItem("saveuser", JSON.stringify(vm.saveuser));
                } else {
                    $window.localStorage.removeItem("saveuser");
                };
                if (loginData == "success") {
                    $location.path("index/index").replace()
                } else if (loginData.name) {
                    // 启用安全验证
                    showSecurity(loginData.value);
                } else {
                    Util.Toast(loginData);
                }
            })
        };
        var showSecurity = function (userId) {
            var param = {};
            param.userId = userId;
            param.clientType = clientType;
            param.clientId = clientId;

            navigator.notification.prompt(
                '请输入6位数字安全码',  // message
                onPrompt,                  // callback to invoke
                '安全验证',            // title
                ['确定', '取消'],             // buttonLabels
                ''                 // defaultText
            );
            function onPrompt(results) {
                if (results.buttonIndex == 1) {
                    if (results.input1) {
                        confirm(results.input1);
                    }else{
                        Util.Toast("请输入安全码")
                    }
                }
            }
            function confirm(safeCode) {
                param.safeCode =safeCode;
                if (!param.safeCode) {
                    Util.Toast("请输入6位安全码")
                    return;
                }
                DataService.getData(openApi.appValidate, param).then(function (data) {
                    if (data.name == "login") {
                        Util.Toast(data.value)
                    } else if (data.name == "validate") {
                        Util.Toast(data.value)
                    } else if (data.name == "index") {
                        $location.path("index/index").replace()
                    }
                });
            }
        }
        var autoLogin = function () {
            // 记住密码是否自动登录
            if (!Util.isNullBbj(userinfo) && vm.saveuser) {
                vm.param = userinfo;
                vm.login();
            }
        }
        var init = function () {
            autoLogin();
        }
        init();
    });
